package component.userInformation;

import lombok.Data;
import org.springframework.stereotype.Component;
import protobuf.protoClass.ProtoMsg;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 用户登录信息
 *
 * @see protobuf.protoClass.ProtoMsg.LoginRequest
 */

@Component("UserAttribute")
@Data
public class UserAttribute {

    /**
     * 对应 {@link ProtoMsg.LoginRequest}序列化
     */
    public static final AtomicInteger number = new AtomicInteger(1);
    String uid = String.valueOf(number.getAndIncrement());   //用户唯一 ID
    String deviceID = UUID.randomUUID().toString();   //设备 ID
    String token = UUID.randomUUID().toString();   //用户 token
    PlatForm platform = PlatForm.WINDOWS;   //登陆平台

    String userName = "defaultUserName";   //用户名
    //private String sessionID;   //session ID

    /**
     * 反序列化
     * 根据 {@link protobuf.protoClass.ProtoMsg.LoginRequest}信息，
     * 设置用户登录信息
     */
    public static UserAttribute makeUserAttribute(ProtoMsg.LoginRequest loginRequest) {
        UserAttribute userAttribute = new UserAttribute();
        userAttribute.uid = loginRequest.getUid();
        userAttribute.deviceID = loginRequest.getDeviceId();
        userAttribute.token = loginRequest.getToken();
        userAttribute.setPlatform(loginRequest.getPlatform());

        System.out.println("登录中。。。" + userAttribute);
        return userAttribute;
    }

    /**
     * 检测登录平台，设置平台类型
     */
    public void setPlatform(int platform) {
        PlatForm[] values = PlatForm.values();
        for (PlatForm value : values) {
            if (value.ordinal() == platform) {
                this.platform = value;
            }
        }
    }

    /**
     * 平台类型
     */
    public enum PlatForm {
        WINDOWS, MAC, ANDROID, IOS, WEB, OTHER;
    }

}
